package com.mayflygeek.mayflyask.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mayflygeek.mayflyask.entity.User;
import com.mayflygeek.mayflyask.service.UserService;
import com.mayflygeek.mayflyask.util.Constants;
/**
 * @author 蝈蝈 
 * @email guodefu909@126.com
 * @team 蜉蝣极客
 * @link www.mayflygeek.com
 * 类说明
 */
@Controller
public class UserController {
	
	private Logger log = LoggerFactory.getLogger(getClass());

	@Resource
	private UserService userService;
	@Resource
	private HttpSession session;
	
	@RequestMapping(value = "/toIndexPage")
	public String toIndexPage() {
		return "indexPage";
	}
	
	@RequestMapping(value = "/userList")
	@ResponseBody  
	public Page<User> getUser(int pageNumber,int pageSize,String userName,String nickName) {
		log.info("/userList");
		return userService.getPageByUserNameNickName(pageNumber, pageSize, userName, nickName);
	}
	
	@RequestMapping(value = "/removeUser")
	@ResponseBody  
	public Map<String, Object> removeUser(String ids) {
		log.info("/removeUser");
		int count = userService.deleteUserByIds(ids);
		Map<String, Object> map = new HashMap<>();
		map.put(Constants.COUNT, count);
		if (count == 0) {
			map.put(Constants.MESSAGE, "没有删除任何数据");
		}else {
			map.put(Constants.MESSAGE, "成功删除"+count+"条数据");
		}
		return map;
	}
	
	@RequestMapping(value = "/saveUser")
	@ResponseBody  
	public Map<String, Object> saveUser(@Valid User user, BindingResult bindingResult) {
		log.info("/saveUser");
		Map<String, Object> map = new HashMap<>();
		if (bindingResult.hasErrors()) {
			List<FieldError> fieldErrors = bindingResult.getFieldErrors();
			Map<String,String> errorMessage = new HashMap<>();
			for (FieldError fieldError : fieldErrors) {
				errorMessage.put(fieldError.getField(), fieldError.getDefaultMessage());
			}
			map.put(Constants.MESSAGE, errorMessage.toString());
		}else {
			try {
				userService.saveUser(user);
				map.put(Constants.COUNT, 1);
				map.put(Constants.MESSAGE, "成功新增1条数据");
			} catch (Exception e) {
				map.put(Constants.MESSAGE, "新增数据失败");
				log.error("保存用户失败！");
				log.error(e.getMessage(), e);
			}
		}
		return map;
	}
	@RequestMapping(value = "/updateUser")
	@ResponseBody  
	public Map<String, Object> updateUser(User user) {
		log.info("/updateUser");
		Map<String, Object> map = new HashMap<>();
		if (user.getId() != null) {
			User userResult = userService.getUser(user.getId());
			if (userResult!=null) {
				userResult.setNickName(user.getNickName());
				userResult.setRole(user.getRole());
				userResult.setSex(user.getSex());
				userResult.setTelephone(user.getTelephone());
				userService.updateUser(userResult);
				map.put(Constants.COUNT, 1);
				map.put(Constants.MESSAGE, "修改成功！");
			}
			
		}
		return map;
	}
	
	@RequestMapping(value = "/updateUserStatus")
	@ResponseBody  
	public Map<String, Object> updateUserStatus(User user) {
		log.info("/updateUserStatus");
		log.info("userId:"+user.getId());
		log.info("userStatus:"+user.getStatus());
		int count;
		Map<String, Object> map = new HashMap<>();
		if (user.getId() != null) {
			count = userService.updateUserStatus(user.getId(), user.getStatus());
			map.put(Constants.COUNT, count);
			map.put(Constants.MESSAGE, "修改成功！");
		}
		return map;
	}
}
